package com.dengdai.applibrary.https;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.entity.ExtJsonEntity;
import com.dengdai.applibrary.interfaces.HttpCallbackListener;
import com.dengdai.applibrary.json.Jackson;
import com.dengdai.applibrary.task.BaseAction;
import com.dengdai.applibrary.task.TaskListener;
import com.dengdai.applibrary.utils.BaseViewUtils;

import java.util.Map;

/**
 * @说明 网络请求工具类
 * @作者 LY
 * @时间 16/5/11 11:08
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public class HttpUtils {

    private static HttpUtils mInstance;// 自身实例

    /**
     * 获取对象实例化
     *
     * @return
     */
    public synchronized static HttpUtils getInstance() {
        if (mInstance == null) {
            mInstance = new HttpUtils();
        }
        return mInstance;
    }

    /**
     * 发送GET请求
     *
     * @param url                  请求地址
     * @param params               请求参数
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    public void sendGet(String url, Map<String, Object> params, Map<String, Object> headParameters, HttpCallbackListener httpCallbackListener) {
        this.send(HttpMethods.GET_METHODS, url, params, headParameters, httpCallbackListener);
    }

    /**
     * 发送POST请求
     *
     * @param url                  请求地址
     * @param params               请求参数
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    public void sendPost(String url, Map<String, Object> params, Map<String, Object> headParameters, HttpCallbackListener httpCallbackListener) {
        this.send(HttpMethods.POST_METHODS, url, params, headParameters, httpCallbackListener);
    }

    /**
     * 发送JSON请求
     *
     * @param url                  请求地址
     * @param params               请求参数
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    public void sendJson(String url, Map<String, Object> params, Map<String, Object> headParameters, HttpCallbackListener httpCallbackListener) {
        this.send(HttpMethods.JSON_METHODS, url, params, headParameters, httpCallbackListener);
    }

    /**
     * 发送DELETE请求
     *
     * @param url                  请求地址
     * @param params               请求参数
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    public void sendDelete(String url, Map<String, Object> params, Map<String, Object> headParameters, HttpCallbackListener httpCallbackListener) {
        this.send(HttpMethods.DELETE_METHODS, url, params, headParameters, httpCallbackListener);
    }

    /**
     * 发送PUT请求
     *
     * @param url                  请求地址
     * @param params               请求参数
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    public void sendPUT(String url, Map<String, Object> params, Map<String, Object> headParameters, HttpCallbackListener httpCallbackListener) {
        this.send(HttpMethods.PUT_METHODS, url, params, headParameters, httpCallbackListener);
    }

    /**
     * @param httpMethods          请求方法
     * @param url                  请求地址
     * @param params               请求地址
     * @param headParameters       请求头部参数
     * @param httpCallbackListener 回调
     */
    private void send(final String httpMethods, final String url, final Map<String, Object> params, final Map<String, Object> headParameters, final HttpCallbackListener httpCallbackListener) {
        final BaseAction baseAction = new BaseAction(BaseApplication.getAppContext()) {
            @Override
            protected void doInbackground() {
                try {
                    String str = null;
                    if (httpMethods.equals(HttpMethods.GET_METHODS)) {
                        str = HttpHelper.getInstance().sendGet(url, params, headParameters);
                    } else if (httpMethods.equals(HttpMethods.POST_METHODS)) {
                        str = HttpHelper.getInstance().sendPost(url, params, headParameters);
                    } else if (httpMethods.equals(HttpMethods.JSON_METHODS)) {
                        str = HttpHelper.getInstance().sendJson(url, params, headParameters);
                    } else if (httpMethods.equals(HttpMethods.DELETE_METHODS)) {
                        str = HttpHelper.getInstance().sendDelete(url, params, headParameters);
                    } else if (httpMethods.equals(HttpMethods.PUT_METHODS)) {
                        str = HttpHelper.getInstance().sendPut(url, params, headParameters);
                    }
                    update(Jackson.parseClass(str, ExtJsonEntity.class));
                } catch (Exception e) {
                    e.printStackTrace();
                    update(e);
                }
            }
        };
        baseAction.setTaskListener(new TaskListener() {
            @Override
            public void onPreExecute() {
                httpCallbackListener.start();
            }

            @Override
            public void onPostExecute() {
                Object object = baseAction.getData();
                if (object != null) {
                    if (object instanceof Exception) {
                        httpCallbackListener.exception((Exception) object);
                    } else if (object instanceof ExtJsonEntity) {
                        ExtJsonEntity extJsonEntity = (ExtJsonEntity) object;
                        if (extJsonEntity.getStatus() == HttpCode.SUCCESSCODE) {
                            httpCallbackListener.success(extJsonEntity);
                        } else {
                            httpCallbackListener.fai(extJsonEntity.getMsg());
                        }
                    }
                } else {
                    httpCallbackListener.fai(BaseApplication.getAppContext().getResources().getString(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.STRING, ResIdName.SERVICE_CONNECT_ERROR)));
                }
            }
        });

        baseAction.execute();
    }
}
